package com.stox.dto.enumeration;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public enum Span {
	DAY_1(Calendar.DATE, 1, "1Day"), 
	WEEK_1(Calendar.WEEK_OF_YEAR, 1, "1Week"), 
	MONTH_1(Calendar.MONTH, 1, "1Month"), 
	QUARTER_1(Calendar.MONTH, 3, "1Quarter"), 
	YEAR_1(Calendar.YEAR, 1, "1Year"), 
	YEAR_2(Calendar.YEAR, 2, "2Years"),
	YEAR_3(Calendar.YEAR, 3, "3Years"), 
	MAX(Calendar.YEAR, 999, "Maximum");

	private final int unit;
	private final int count;
	private final String code;

	private Span(int unit, int count, String code) {
		this.unit = unit;
		this.count = count;
		this.code = code;
	}

	public int getCount() {
		return count;
	}

	public int getUnit() {
		return unit;
	}

	public String getCode() {
		return code;
	}
	
	@Override
	public String toString() {
		return getCode();
	}
	
	public Date getStartDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(unit, -1 * count);
		return calendar.getTime();
	}

	public List<SpanType> getSpanTypes() {
		List<SpanType> spanTypes = new ArrayList<SpanType>();
		for (SpanType spanType : SpanType.values()) {
			if (spanType.getUnit() > getUnit()) {
				if (ordinal() >= QUARTER_1.ordinal()
						&& spanType.ordinal() <= SpanType.HOURLY.ordinal()) {
					continue;
				}
				spanTypes.add(spanType);
			}
		}
		return spanTypes;
	}
}
